﻿using ChuanYe.WorkLogManagement.EF.SqlServer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ChuanYe.WorkLogManagement.BLL
{
    public class DepartmentBLL
    {
        #region 构造单例
        private DepartmentBLL() { }
        private static DepartmentBLL _instance;
        public static DepartmentBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new DepartmentBLL());
            }
        }
        #endregion

        public dynamic Get(int departmentId,string departmentName)
        {
            using (WorkLogContext context = new WorkLogContext())
            {
                var linq = from a in context.DepartmentInfo 
                           select  a;
                if (departmentId > 0)
                {
                    linq = linq.Where(p => p.DepartmentID == departmentId);
                }
                if (!string.IsNullOrEmpty(departmentName))
                {
                    linq = linq.Where(p => p.DepartmentName == departmentName);
                }
                return linq.FirstOrDefault();
            }
        }

        public List<dynamic> Get()
        {
            using (WorkLogContext context = new WorkLogContext())
            {
                var linq = from a in context.DepartmentInfo
                           select a;
                return linq.OrderBy(o=>o.DepartmentID).ToList<dynamic>();
            }
        }

        public int Add(DepartmentInfo info)
        {
            using (WorkLogContext context = new WorkLogContext())
            {
                context.DepartmentInfo.Add(info);
                context.Entry<DepartmentInfo>(info).State = EntityState.Added;
                context.SaveChanges();
                return info.DepartmentID ;
            }
        }
    }
}
